/*
 * 文件名：QuartzController.java
 * 版权：Copyright by Layne
 * 描述：
 * 修改人：Layne
 * 修改时间：2019年5月23日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.layne.boot.samples.quartz.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import top.layne.boot.samples.quartz.entity.Message;
import top.layne.boot.samples.quartz.entity.QuartzInfo;
import top.layne.boot.samples.quartz.service.IQuartzService;

@RestController
@RequestMapping("/quartz")
public class QuartzController {
	private static final Logger LOGGER = LoggerFactory.getLogger(QuartzController.class);

	@Resource
	private IQuartzService iQuartzService;

	/**
	 * 插入任务
	 * 
	 * @param quartzInfo
	 * @return
	 * @see
	 */
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public Object insert(QuartzInfo quartzInfo) {
		Message message = new Message();
		try {
			message.setResult(iQuartzService.insert(quartzInfo));
		} catch (Exception e) {
			LOGGER.error("insert method error :", e);
			message.setCode(500);
			message.setMessage("insert error!");
		}
		return message;
	}

	/**
	 * 更新任务
	 * 
	 * @param quartzInfo
	 * @return
	 * @see
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public Object update(QuartzInfo quartzInfo) {
		Message message = new Message();
		try {
			message.setResult(iQuartzService.update(quartzInfo));
		} catch (Exception e) {
			LOGGER.error("update method error :", e);
			message.setCode(500);
			message.setMessage("update error!");
		}
		return message;
	}

	/**
	 * 删除任务
	 * 
	 * @param quartzInfo
	 * @return
	 * @see
	 */
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public Object delete(QuartzInfo quartzInfo) {
		Message message = new Message();
		try {
			message.setResult(iQuartzService.delete(quartzInfo));
		} catch (Exception e) {
			LOGGER.error("delete method error :", e);
			message.setCode(500);
			message.setMessage("delete error!");
		}
		return message;
	}

	/**
	 * 暂停任务
	 * 
	 * @param quartzInfo
	 * @return
	 * @see
	 */
	@RequestMapping(value = "pause", method = RequestMethod.GET)
	public Object pause(QuartzInfo quartzInfo) {
		Message message = new Message();
		try {
			message.setResult(iQuartzService.pause(quartzInfo));
		} catch (Exception e) {
			LOGGER.error("pause method error :", e);
			message.setCode(500);
			message.setMessage("pause error!");
		}
		return message;
	}

	/**
	 * 恢复任务
	 * 
	 * @param quartzInfo
	 * @return
	 * @see
	 */
	@RequestMapping(value = "resume", method = RequestMethod.GET)
	public Object resume(QuartzInfo quartzInfo) {
		Message message = new Message();
		try {
			message.setResult(iQuartzService.resume(quartzInfo));
		} catch (Exception e) {
			LOGGER.error("resume method error :", e);
			message.setCode(500);
			message.setMessage("resume error!");
		}
		return message;
	}

	/**
	 * 获取所有任务列表
	 * 
	 * @return
	 * @see
	 */
	@RequestMapping(value = "query", method = RequestMethod.GET)
	public Object query() {
		Message message = new Message();
		try {
			message.setResult(iQuartzService.query());
		} catch (Exception e) {
			LOGGER.error("query method error :", e);
			message.setCode(500);
			message.setMessage("query error!");
		}
		return message;
	}
}
